## MessageCenter 使用示例
## 演示各种订阅和发送消息的方式

extends Node

# 玩家数据示例
class Player:
	var name: String
	var score: int
	
	func _init(n: String = "Player", s: int = 0):
		name = n
		score = s


func _ready():
	print("\n========== MessageCenter 使用示例 ==========\n")
	
	# 示例1: 基础订阅和发送
	example_basic()
	
	# 示例2: 不同参数数量的回调
	example_parameters()
	
	# 示例3: 匿名订阅
	example_anonymous()
	
	# 示例4: 多订阅者
	example_multiple_subscribers()
	
	# 示例5: 取消订阅
	example_unsubscribe()
	
	# 示例6: 实际游戏场景
	example_game_scenario()


## 示例1: 基础订阅和发送
func example_basic():
	print("=== 示例1: 基础订阅和发送 ===")
	
	# 订阅消息
	MessageCenter.subscribe(self, "test_channel", _on_test_message)
	
	# 发送消息
	MessageCenter.send(self, "test_channel", "Hello MessageCenter!")
	
	# 清理
	MessageCenter.unsubscribe(self, "test_channel")
	print()


func _on_test_message(sender, data):
	print("收到消息 - 发送者: ", sender, ", 数据: ", data)


## 示例2: 不同参数数量的回调
func example_parameters():
	print("=== 示例2: 不同参数数量的回调 ===")
	
	# 双参数回调（默认）
	MessageCenter.subscribe(self, "two_params", func(sender, data):
		print("双参数回调 - sender: ", sender.name if sender is Player else sender, ", data: ", data)
	, 2)
	
	# 单参数回调（只接收sender）
	MessageCenter.subscribe(self, "one_param", func(sender):
		print("单参数回调 - sender: ", sender.name if sender is Player else sender)
	, 1)
	
	var player = Player.new("Alice", 100)
	MessageCenter.send(player, "two_params", {"message": "双参数测试"})
	MessageCenter.send(player, "one_param", {"message": "这个数据会被忽略"})
	
	# 清理
	MessageCenter.unsubscribe(self, "two_params")
	MessageCenter.unsubscribe(self, "one_param")
	print()


## 示例3: 匿名订阅
func example_anonymous():
	print("=== 示例3: 匿名订阅 ===")
	
	# 匿名订阅，返回订阅者对象
	var subscriber1 = MessageCenter.subscribe_anonymous("event_channel", func(sender, data):
		print("匿名订阅者1收到: ", data)
	)
	
	var subscriber2 = MessageCenter.subscribe_anonymous("event_channel", func(sender, data):
		print("匿名订阅者2收到: ", data)
	)
	
	MessageCenter.send(self, "event_channel", "发送给匿名订阅者")
	
	# 取消其中一个匿名订阅
	MessageCenter.unsubscribe(subscriber1, "event_channel")
	print("取消订阅者1后:")
	MessageCenter.send(self, "event_channel", "只有订阅者2能收到")
	
	# 清理
	MessageCenter.unsubscribe(subscriber2, "event_channel")
	print()


## 示例4: 多订阅者
func example_multiple_subscribers():
	print("=== 示例4: 多订阅者 ===")
	
	# 模拟多个订阅者
	var listener1 = Node.new()
	listener1.name = "Listener1"
	var listener2 = Node.new()
	listener2.name = "Listener2"
	var listener3 = Node.new()
	listener3.name = "Listener3"
	
	MessageCenter.subscribe(listener1, "broadcast", func(sender, data):
		print("  [Listener1] 收到广播: ", data)
	)
	MessageCenter.subscribe(listener2, "broadcast", func(sender, data):
		print("  [Listener2] 收到广播: ", data)
	)
	MessageCenter.subscribe(listener3, "broadcast", func(sender, data):
		print("  [Listener3] 收到广播: ", data)
	)
	
	print("当前订阅数: ", MessageCenter.get_subscription_count("broadcast"))
	MessageCenter.send(self, "broadcast", "广播消息给所有人")
	
	# 清理
	MessageCenter.unsubscribe_all(listener1)
	MessageCenter.unsubscribe_all(listener2)
	MessageCenter.unsubscribe_all(listener3)
	listener1.free()
	listener2.free()
	listener3.free()
	print()


## 示例5: 取消订阅
func example_unsubscribe():
	print("=== 示例5: 取消订阅 ===")
	
	MessageCenter.subscribe(self, "channel1", func(s, d): print("Channel1: ", d))
	MessageCenter.subscribe(self, "channel2", func(s, d): print("Channel2: ", d))
	MessageCenter.subscribe(self, "channel3", func(s, d): print("Channel3: ", d))
	
	print("订阅了3个频道，发送消息:")
	MessageCenter.send(self, "channel1", "消息1")
	MessageCenter.send(self, "channel2", "消息2")
	MessageCenter.send(self, "channel3", "消息3")
	
	print("\n取消channel2订阅后:")
	MessageCenter.unsubscribe(self, "channel2")
	MessageCenter.send(self, "channel1", "消息1-2")
	MessageCenter.send(self, "channel2", "这条消息不会被接收")
	MessageCenter.send(self, "channel3", "消息3-2")
	
	# 清理
	MessageCenter.unsubscribe_all(self)
	print()


## 示例6: 实际游戏场景
func example_game_scenario():
	print("=== 示例6: 实际游戏场景 ===")
	
	# 模拟UI订阅玩家事件
	var ui_manager = Node.new()
	ui_manager.name = "UIManager"
	MessageCenter.subscribe(ui_manager, "player_score_changed", func(player, data):
		print("  [UI] 更新分数显示: ", player.name, " -> ", data.score)
	)
	MessageCenter.subscribe(ui_manager, "player_died", func(player, data):
		print("  [UI] 显示死亡界面: ", player.name)
	)
	
	# 模拟音效管理器订阅
	var audio_manager = Node.new()
	audio_manager.name = "AudioManager"
	MessageCenter.subscribe(audio_manager, "player_score_changed", func(player, data):
		print("  [Audio] 播放得分音效")
	)
	MessageCenter.subscribe(audio_manager, "player_died", func(player, data):
		print("  [Audio] 播放死亡音效")
	)
	
	# 模拟成就系统订阅
	var achievement_system = Node.new()
	achievement_system.name = "AchievementSystem"
	MessageCenter.subscribe(achievement_system, "player_score_changed", func(player, data):
		if data.score >= 1000:
			print("  [Achievement] 解锁成就: 得分达到1000!")
	)
	
	# 模拟游戏事件
	print("\n玩家得分:")
	var player = Player.new("Bob", 0)
	player.score = 500
	MessageCenter.send(player, "player_score_changed", {"score": 500})
	
	print("\n玩家继续得分:")
	player.score = 1200
	MessageCenter.send(player, "player_score_changed", {"score": 1200})
	
	print("\n玩家死亡:")
	MessageCenter.send(player, "player_died", {"reason": "碰到敌人"})
	
	# 清理
	MessageCenter.unsubscribe_all(ui_manager)
	MessageCenter.unsubscribe_all(audio_manager)
	MessageCenter.unsubscribe_all(achievement_system)
	ui_manager.free()
	audio_manager.free()
	achievement_system.free()
	print()
	
	print("========== 示例结束 ==========\n")
